/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.user.service.impl;

import java.sql.Connection;
import java.util.List;

import net.comm.page.PageCtr;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.util.DatetimeUtil;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.common.common;

import net.comm.user.bean.BaseVistorLog;
import net.comm.user.db.DbVistorLogService;
import net.comm.user.db.VistorLogServiceSqlMysql;
import net.comm.user.web.form.VistorLogForm;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class VistorLogServiceImpl {

	static Logger logger = Logger.getLogger(VistorLogServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(VistorLogForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			VistorLogServiceSqlMysql servSql = new VistorLogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			
			fields2[0] = theForm.getQryUserID();

			String sql = servSql.getCountSql2();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields2));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getQryUserID();
			fields[1] = page.getPos()-1;
			fields[2] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();

			list = bSqlServ.dbRead(cn, sql, fields, new DbVistorLogService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}
	/**
	 * 返回根据会员ID查询的会员操作日志
	 */
	public List list(int  userID, int len) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];
			
			fields[0] = userID;			
			fields[1] = len;			

			VistorLogServiceSqlMysql servSql = new VistorLogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByUserIDSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbVistorLogService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(VistorLogForm theForm, ReturnMessage msg) 
	{

		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			get(cn, theForm, msg);			

			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);			
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}	
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void add(VistorLogForm theForm, ReturnMessage msg) 
	{

		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);
			
			add(cn, theForm, msg); 
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);			
			DbcpPool.returnConnection(cn);			
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}	
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void add(Connection cn, VistorLogForm theForm, ReturnMessage msg) 
	{

		try
		{
			
			VistorLogServiceSqlMysql servSql = new VistorLogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//判断是否已经存在同一访问者记录 存在更新该记录的访问时间
			Object[] fields = new Object[2];
			
			fields[0] = theForm.getUserID();
			fields[1] = theForm.getVistorID();
			
			String sql = servSql.getRecbyMtimeSql();
			
			List list = bSqlServ.dbRead(cn, sql, fields, new DbVistorLogService());
			
			if(list.size() > 0 ){
				
				Object[] fields3 = new Object[3];
				
				fields3[0] = DatetimeUtil.getDatetime();
				fields3[1] = theForm.getUserID();
				fields3[2] = theForm.getVistorID();

				sql = servSql.getUpdbyVistorIDSql();
				
				boolean  res = bSqlServ.dbWrite(cn, sql, fields3);
				if(!res){
					common.setAppFailMsg(msg , " 插入记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}				
			}else{
				theForm.setSeqID(SequenceMgr.generateSeq( GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_VISTORLOGID));
				
				Object[] fields2 = new Object[4];
				
				fields2[0] = theForm.getSeqID();
				fields2[1] = theForm.getUserID();
				fields2[2] = theForm.getVistorID();
				fields2[3] = DatetimeUtil.getDatetime();

				sql = servSql.getInsertSql();
				
				boolean  res = bSqlServ.dbWrite(cn, sql, fields2);
				if(!res){
					common.setAppFailMsg(msg , " 插入记录失败，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;
				}				
			}
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}	
		} 
		
		return ;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(Connection cn, VistorLogForm theForm, ReturnMessage msg) 
	{

		try
		{
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getSeqID();
						
			VistorLogServiceSqlMysql servSql = new VistorLogServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();
						
			List list = bSqlServ.dbRead(cn, sql, fields, new DbVistorLogService());
			
			if(list.size() == 0){
				common.setAppFailMsg(msg , " 记录不存在，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;					
			}
			BaseVistorLog base = (BaseVistorLog)list.get(0);
			BeanUtils.copyProperties(theForm, base);
			
		
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}	
		} 
		
		return ;
	}
	
	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(VistorLogForm theForm, ReturnMessage msg) 
	{
		
		return ;
	}

	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(VistorLogForm theForm, ReturnMessage msg) 
	{
				
		return ;
	}

}
